
% Default to the notebook output style

    


% Inherit from the specified cell style.




    
\documentclass[11pt]{article}

    
    
    \usepackage[T1]{fontenc}
    % Nicer default font (+ math font) than Computer Modern for most use cases
    \usepackage{mathpazo}

    % Basic figure setup, for now with no caption control since it's done
    % automatically by Pandoc (which extracts ![](path) syntax from Markdown).
    \usepackage{graphicx}
    % We will generate all images so they have a width \maxwidth. This means
    % that they will get their normal width if they fit onto the page, but
    % are scaled down if they would overflow the margins.
    \makeatletter
    \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth
    \else\Gin@nat@width\fi}
    \makeatother
    \let\Oldincludegraphics\includegraphics
    % Set max figure width to be 80% of text width, for now hardcoded.
    \renewcommand{\includegraphics}[1]{\Oldincludegraphics[width=.8\maxwidth]{#1}}
    % Ensure that by default, figures have no caption (until we provide a
    % proper Figure object with a Caption API and a way to capture that
    % in the conversion process - todo).
    \usepackage{caption}
    \DeclareCaptionLabelFormat{nolabel}{}
    \captionsetup{labelformat=nolabel}

    \usepackage{adjustbox} % Used to constrain images to a maximum size 
    \usepackage{xcolor} % Allow colors to be defined
    \usepackage{enumerate} % Needed for markdown enumerations to work
    \usepackage{geometry} % Used to adjust the document margins
    \usepackage{amsmath} % Equations
    \usepackage{amssymb} % Equations
    \usepackage{textcomp} % defines textquotesingle
    % Hack from http://tex.stackexchange.com/a/47451/13684:
    \AtBeginDocument{%
        \def\PYZsq{\textquotesingle}% Upright quotes in Pygmentized code
    }
    \usepackage{upquote} % Upright quotes for verbatim code
    \usepackage{eurosym} % defines \euro
    \usepackage[mathletters]{ucs} % Extended unicode (utf-8) support
    \usepackage[utf8x]{inputenc} % Allow utf-8 characters in the tex document
    \usepackage{fancyvrb} % verbatim replacement that allows latex
    \usepackage{grffile} % extends the file name processing of package graphics 
                         % to support a larger range 
    % The hyperref package gives us a pdf with properly built
    % internal navigation ('pdf bookmarks' for the table of contents,
    % internal cross-reference links, web links for URLs, etc.)
    \usepackage{hyperref}
    \usepackage{longtable} % longtable support required by pandoc >1.10
    \usepackage{booktabs}  % table support for pandoc > 1.12.2
    \usepackage[inline]{enumitem} % IRkernel/repr support (it uses the enumerate* environment)
    \usepackage[normalem]{ulem} % ulem is needed to support strikethroughs (\sout)
                                % normalem makes italics be italics, not underlines
    

    
    
    % Colors for the hyperref package
    \definecolor{urlcolor}{rgb}{0,.145,.698}
    \definecolor{linkcolor}{rgb}{.71,0.21,0.01}
    \definecolor{citecolor}{rgb}{.12,.54,.11}

    % ANSI colors
    \definecolor{ansi-black}{HTML}{3E424D}
    \definecolor{ansi-black-intense}{HTML}{282C36}
    \definecolor{ansi-red}{HTML}{E75C58}
    \definecolor{ansi-red-intense}{HTML}{B22B31}
    \definecolor{ansi-green}{HTML}{00A250}
    \definecolor{ansi-green-intense}{HTML}{007427}
    \definecolor{ansi-yellow}{HTML}{DDB62B}
    \definecolor{ansi-yellow-intense}{HTML}{B27D12}
    \definecolor{ansi-blue}{HTML}{208FFB}
    \definecolor{ansi-blue-intense}{HTML}{0065CA}
    \definecolor{ansi-magenta}{HTML}{D160C4}
    \definecolor{ansi-magenta-intense}{HTML}{A03196}
    \definecolor{ansi-cyan}{HTML}{60C6C8}
    \definecolor{ansi-cyan-intense}{HTML}{258F8F}
    \definecolor{ansi-white}{HTML}{C5C1B4}
    \definecolor{ansi-white-intense}{HTML}{A1A6B2}

    % commands and environments needed by pandoc snippets
    % extracted from the output of `pandoc -s`
    \providecommand{\tightlist}{%
      \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
    \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
    % Add ',fontsize=\small' for more characters per line
    \newenvironment{Shaded}{}{}
    \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
    \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}}
    \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
    \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
    \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
    \newcommand{\CharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\StringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textit{{#1}}}}
    \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}}
    \newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
    \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}}
    \newcommand{\RegionMarkerTok}[1]{{#1}}
    \newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.00,0.00}{\textbf{{#1}}}}
    \newcommand{\NormalTok}[1]{{#1}}
    
    % Additional commands for more recent versions of Pandoc
    \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.53,0.00,0.00}{{#1}}}
    \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
    \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.73,0.40,0.53}{{#1}}}
    \newcommand{\ImportTok}[1]{{#1}}
    \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{{#1}}}}
    \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.10,0.09,0.49}{{#1}}}
    \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.44,0.13}{\textbf{{#1}}}}
    \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.40,0.40,0.40}{{#1}}}
    \newcommand{\BuiltInTok}[1]{{#1}}
    \newcommand{\ExtensionTok}[1]{{#1}}
    \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.74,0.48,0.00}{{#1}}}
    \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.49,0.56,0.16}{{#1}}}
    \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.38,0.63,0.69}{\textbf{\textit{{#1}}}}}
    
    
    % Define a nice break command that doesn't care if a line doesn't already
    % exist.
    \def\br{\hspace*{\fill} \\* }
    % Math Jax compatability definitions
    \def\gt{>}
    \def\lt{<}
    % Document parameters
    \title{Python\_learning\_cons}
    
    
    

    % Pygments definitions
    
\makeatletter
\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax%
    \let\PY@ul=\relax \let\PY@tc=\relax%
    \let\PY@bc=\relax \let\PY@ff=\relax}
\def\PY@tok#1{\csname PY@tok@#1\endcsname}
\def\PY@toks#1+{\ifx\relax#1\empty\else%
    \PY@tok{#1}\expandafter\PY@toks\fi}
\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{%
    \PY@it{\PY@bf{\PY@ff{#1}}}}}}}
\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}}

\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}}
\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}}
\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}}
\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}}
\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}}
\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}}
\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}}
\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}}
\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}}
\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit}
\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf}
\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}}
\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}}
\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}
\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\expandafter\def\csname PY@tok@fm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@vm\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\expandafter\def\csname PY@tok@sa\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@dl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PY@tok@ch\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@cpf\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}
\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}}

\def\PYZbs{\char`\\}
\def\PYZus{\char`\_}
\def\PYZob{\char`\{}
\def\PYZcb{\char`\}}
\def\PYZca{\char`\^}
\def\PYZam{\char`\&}
\def\PYZlt{\char`\<}
\def\PYZgt{\char`\>}
\def\PYZsh{\char`\#}
\def\PYZpc{\char`\%}
\def\PYZdl{\char`\$}
\def\PYZhy{\char`\-}
\def\PYZsq{\char`\'}
\def\PYZdq{\char`\"}
\def\PYZti{\char`\~}
% for compatibility with earlier versions
\def\PYZat{@}
\def\PYZlb{[}
\def\PYZrb{]}
\makeatother


    % Exact colors from NB
    \definecolor{incolor}{rgb}{0.0, 0.0, 0.5}
    \definecolor{outcolor}{rgb}{0.545, 0.0, 0.0}



    
    % Prevent overflowing lines due to hard-to-break entities
    \sloppy 
    % Setup hyperref package
    \hypersetup{
      breaklinks=true,  % so long urls are correctly broken across lines
      colorlinks=true,
      urlcolor=urlcolor,
      linkcolor=linkcolor,
      citecolor=citecolor,
      }
    % Slightly bigger margins than the latex defaults
    
    \geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
    
    

    \begin{document}
    
    
    \maketitle
    
    

    
    \section{Python 控制语句}\label{python-ux63a7ux5236ux8bedux53e5}

    控制语句（statements）是一个程序能够运行的基本保障，它通过与表达式（expressions）结合构成了Python程序的基本结构。Python
所含有的基本控制语句总结如下：

    \begin{figure}
\centering
\includegraphics{attachment:1530268055\%281\%29.png}
\caption{1530268055\%281\%29.png}
\end{figure}

    \begin{figure}
\centering
\includegraphics{attachment:1530268101\%281\%29.png}
\caption{1530268101\%281\%29.png}
\end{figure}

    Python常用控制语句，图片来源：Mark Lutz, Learning Python 5th Edition,
p.p.320 - 321

    \textbf{注意：其中条件语句和控制语句的括号是非必须的。即可以是if(x
\textgreater{} y):或if x \textgreater{}
y:。以下是一个循环语句的使用示例。}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}4}]:} \PY{k}{while} \PY{k+kc}{True}\PY{p}{:}
            \PY{n}{msg} \PY{o}{=} \PY{n+nb}{input}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Input a nmber please! Number: }\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
            
            \PY{k}{try}\PY{p}{:}
                \PY{k}{if} \PY{n}{msg} \PY{o}{==} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{stop}\PY{l+s+s1}{\PYZsq{}} \PY{o+ow}{or} \PY{n}{msg} \PY{o}{==} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{停止}\PY{l+s+s1}{\PYZsq{}}\PY{p}{:}
                    \PY{n+nb}{print}\PY{p}{(}\PY{n}{msg}\PY{o}{.}\PY{n}{upper}\PY{p}{(}\PY{p}{)}\PY{p}{)}
                    \PY{k}{break}
                \PY{n}{digits} \PY{o}{=} \PY{n+nb}{eval}\PY{p}{(}\PY{n}{msg}\PY{p}{)}
            \PY{k}{except}\PY{p}{:}
                \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Not number! Try again!}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
            \PY{k}{else}\PY{p}{:}
                \PY{n+nb}{print}\PY{p}{(}\PY{n}{digits} \PY{o}{*}\PY{o}{*} \PY{l+m+mi}{2}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
Input a nmber please! Number: 5
25
Input a nmber please! Number: 5.5
30.25
Input a nmber please! Number: hi
Not number! Try again!
Input a nmber please! Number: stop
STOP

    \end{Verbatim}

    Python中允许控制语句中出现表达式（expressions），但是不允许控制语句被作为表达式使用。比如在while循环中不允许使用单等号。

    \subsection{赋值语句}\label{ux8d4bux503cux8bedux53e5}

    \subsubsection{赋值语句的不同形态}\label{ux8d4bux503cux8bedux53e5ux7684ux4e0dux540cux5f62ux6001}

    Python 3
一共支持7种不同形态的赋值语句，Python中赋值语句的形态是相当丰富的。

    \begin{figure}
\centering
\includegraphics{attachment:1530269679\%281\%29.png}
\caption{1530269679\%281\%29.png}
\end{figure}

    Python赋值语句不同形态总结，图片来源：Mark Lutz, Learning Python 5th
Edition, p.p.340

    其中第三、四种序列型赋值以及第五种解包型赋值（unpacked
assignments）示例如下：

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}2}]:} \PY{n}{spam} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{joke}\PY{l+s+s1}{\PYZsq{}}
        \PY{n}{ham} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{joe}\PY{l+s+s1}{\PYZsq{}}
        \PY{p}{[}\PY{n}{spam}\PY{p}{,} \PY{n}{ham}\PY{p}{]} \PY{o}{=} \PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{yum}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{YUM}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{spam}\PY{p}{,} \PY{n}{ham}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
yum YUM

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}6}]:} \PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{,} \PY{n}{d} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{spam}\PY{l+s+s1}{\PYZsq{}}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{,} \PY{n}{d}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
s p a m

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}8}]:} \PY{c+c1}{\PYZsh{} The \PYZdq{}first\PYZhy{}rest pattern\PYZdq{}}
        \PY{n}{a}\PY{p}{,} \PY{o}{*}\PY{n}{b} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{spam}\PY{l+s+s1}{\PYZsq{}}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{o}{*}\PY{n}{b}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
s ['p', 'a', 'm']
p a m

    \end{Verbatim}

    使用元组（或列表）对一些列变量同时赋值时，需要保证等号左右两边变量和对象数量一致，或在使用解包符号（``
*
''）的情况下左边变量数量可以小于右边对象数量。然而，``数量一致可以有多种形式''。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}1}]:} \PY{n}{string} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Today}\PY{l+s+s1}{\PYZsq{}}
        \PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c} \PY{o}{=} \PY{n}{string}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{,} \PY{n}{string}\PY{p}{[}\PY{l+m+mi}{2}\PY{p}{]}\PY{p}{,} \PY{n}{string}\PY{p}{[}\PY{l+m+mi}{3}\PY{p}{:}\PY{p}{]}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
o d ay

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}5}]:} \PY{n}{string} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Today}\PY{l+s+s1}{\PYZsq{}}
        \PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c} \PY{o}{=} \PY{o}{*}\PY{n}{string}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{:}\PY{l+m+mi}{3}\PY{p}{]}\PY{p}{,} \PY{n}{string}\PY{p}{[}\PY{l+m+mi}{3}\PY{p}{:}\PY{p}{]}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
o d ay

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}1}]:} \PY{n}{string} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Today}\PY{l+s+s1}{\PYZsq{}}
        \PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c} \PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{n}{string}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{:}\PY{l+m+mi}{3}\PY{p}{]}\PY{p}{)} \PY{o}{+} \PY{p}{[}\PY{n}{string}\PY{p}{[}\PY{l+m+mi}{3}\PY{p}{:}\PY{p}{]}\PY{p}{]}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
o d ay

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}9}]:} \PY{n}{string} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Today}\PY{l+s+s1}{\PYZsq{}}
        \PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{)}\PY{p}{,} \PY{n}{c} \PY{o}{=} \PY{n}{string}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{:}\PY{l+m+mi}{3}\PY{p}{]}\PY{p}{,} \PY{n}{string}\PY{p}{[}\PY{l+m+mi}{3}\PY{p}{:}\PY{p}{]}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
o d ay

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}10}]:} \PY{n}{string} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Today}\PY{l+s+s1}{\PYZsq{}}
         \PY{p}{(}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{)}\PY{p}{,} \PY{n}{c}\PY{p}{)} \PY{o}{=} \PY{n}{string}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{:}\PY{l+m+mi}{3}\PY{p}{]}\PY{p}{,} \PY{n}{string}\PY{p}{[}\PY{l+m+mi}{3}\PY{p}{:}\PY{p}{]}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
o d ay

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}25}]:} \PY{c+c1}{\PYZsh{} The \PYZdq{}first\PYZhy{}rest\PYZhy{}last\PYZdq{} pattern}
         \PY{n}{a}\PY{p}{,} \PY{o}{*}\PY{n}{b}\PY{p}{,} \PY{n}{c} \PY{o}{=} \PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{5}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
0 [1, 2, 3] 4

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}24}]:} \PY{n}{list1} \PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{tobeornottobe}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
         
         \PY{k}{while} \PY{n}{list1}\PY{p}{:}
             \PY{n}{char}\PY{p}{,} \PY{o}{*}\PY{n}{list1} \PY{o}{=} \PY{n}{list1}
             \PY{c+c1}{\PYZsh{} This is same as:}
             \PY{c+c1}{\PYZsh{} char, list1 = list1[0], list1[1:]}
             \PY{c+c1}{\PYZsh{} which ensures that the list shrinks.}
             \PY{n+nb}{print}\PY{p}{(}\PY{n}{char}\PY{p}{,} \PY{n}{list1}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
t ['o', 'b', 'e', 'o', 'r', 'n', 'o', 't', 't', 'o', 'b', 'e']
o ['b', 'e', 'o', 'r', 'n', 'o', 't', 't', 'o', 'b', 'e']
b ['e', 'o', 'r', 'n', 'o', 't', 't', 'o', 'b', 'e']
e ['o', 'r', 'n', 'o', 't', 't', 'o', 'b', 'e']
o ['r', 'n', 'o', 't', 't', 'o', 'b', 'e']
r ['n', 'o', 't', 't', 'o', 'b', 'e']
n ['o', 't', 't', 'o', 'b', 'e']
o ['t', 't', 'o', 'b', 'e']
t ['t', 'o', 'b', 'e']
t ['o', 'b', 'e']
o ['b', 'e']
b ['e']
e []

    \end{Verbatim}

    \subsubsection{多变量赋同值}\label{ux591aux53d8ux91cfux8d4bux540cux503c}

    多变量赋同值时，若赋值对象为不可变对象（immutable
object），则一个变量中对象的改变不影响其他变量。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}35}]:} \PY{n}{a} \PY{o}{=} \PY{n}{b} \PY{o}{=} \PY{n}{c} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{goodday}\PY{l+s+s1}{\PYZsq{}}
         \PY{n}{a} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{welcome}\PY{l+s+s1}{\PYZsq{}}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
welcome goodday goodday

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}36}]:} \PY{n}{a} \PY{o}{=} \PY{n}{b} \PY{o}{=} \PY{l+m+mi}{100}
         \PY{n}{a} \PY{o}{+}\PY{o}{=} \PY{l+m+mi}{1}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{b}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
100

    \end{Verbatim}

    然而对于可变对象来说，该赋值方式可能就会产生第一章``共享内存''部分所说问题。因此，如果需要用变量表示可变对象时，推荐将每个变量分别赋值。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}37}]:} \PY{n}{lst1} \PY{o}{=} \PY{n}{lst2} \PY{o}{=} \PY{p}{[}\PY{p}{]}
         \PY{n}{lst1}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{l+m+mi}{666}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{lst2}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
[666]

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}41}]:} \PY{n}{lst1} \PY{o}{=} \PY{p}{[}\PY{p}{]}\PY{p}{;} \PY{n}{lst2} \PY{o}{=} \PY{p}{[}\PY{p}{]}
         \PY{c+c1}{\PYZsh{} Better way to write this:}
         \PY{c+c1}{\PYZsh{} lst1, lst2 = [], []}
         \PY{n}{lst1}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{l+m+mi}{666}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{lst2}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
[]

    \end{Verbatim}

    但是，对于两个多变量赋予相同列表值进行`+'或`*
'操作除外，因为这两个操作创建了新对象。而`` * = ''则和`` +=
''与这个情况相比又不一样了，因为它们只是直接改变对应地址的值。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}44}]:} \PY{n}{L1} \PY{o}{=} \PY{n}{L2} \PY{o}{=} \PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{g}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}
         \PY{n}{L1} \PY{o}{=} \PY{n}{L1} \PY{o}{+} \PY{n+nb}{list}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{2312}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{L1}\PY{p}{,} \PY{n}{L2}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
['g', 'b', '2', '3', '1', '2'] ['g', 'b']

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}45}]:} \PY{n}{L1} \PY{o}{=} \PY{n}{L2} \PY{o}{=} \PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{g}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}
         \PY{n}{L1} \PY{o}{+}\PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{2312}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{L1}\PY{p}{,} \PY{n}{L2}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
['g', 'b', '2', '3', '1', '2'] ['g', 'b', '2', '3', '1', '2']

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}46}]:} \PY{n}{L1} \PY{o}{=} \PY{n}{L2} \PY{o}{=} \PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{g}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}
         \PY{n}{L1} \PY{o}{=} \PY{n}{L1} \PY{o}{*} \PY{l+m+mi}{4}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{L1}\PY{p}{,} \PY{n}{L2}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
['g', 'b', 'g', 'b', 'g', 'b', 'g', 'b'] ['g', 'b']

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}47}]:} \PY{n}{L1} \PY{o}{=} \PY{n}{L2} \PY{o}{=} \PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{g}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}
         \PY{n}{L1} \PY{o}{*}\PY{o}{=} \PY{l+m+mi}{4}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{L1}\PY{p}{,} \PY{n}{L2}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
['g', 'b', 'g', 'b', 'g', 'b', 'g', 'b'] ['g', 'b', 'g', 'b', 'g', 'b', 'g', 'b']

    \end{Verbatim}

    \subsubsection{增强型赋值}\label{ux589eux5f3aux578bux8d4bux503c}

    与许多其他编程语言一样，Python也支持一定程度的增强型赋值。

    \begin{figure}
\centering
\includegraphics{attachment:1530329853\%281\%29.png}
\caption{1530329853\%281\%29.png}
\end{figure}

    Python支持的增强型赋值，图片来源：Mark Lutz, Learning Python 5th
Edition, p.p.350

    **备注1：Python支持的增强型赋值有的可以应用于对象，比如+=和*=可以应用于列表或字符串。**

    \textbf{备注2：Python不支持传统的++、-\/-等歧义性较大的增强型赋值，统一写成诸如a
+= 1的形式。}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}43}]:} \PY{n}{s} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{love letter: love you! }\PY{l+s+s1}{\PYZsq{}}
         \PY{n}{s} \PY{o}{*}\PY{o}{=} \PY{l+m+mi}{4}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{s}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
love letter: love you! love letter: love you! love letter: love you! love letter: love you! 

    \end{Verbatim}

    \subsubsection{赋值语句注意事项}\label{ux8d4bux503cux8bedux53e5ux6ce8ux610fux4e8bux9879}

    在使用赋值语句时，建议避免对于不返回值的方法或函数进行赋值，因为它们导致被赋值变量被直接重写为None。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}67}]:} \PY{n}{lst} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{]}
         \PY{n}{lst} \PY{o}{=} \PY{n}{lst}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{l+m+mi}{5}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{lst}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
None

    \end{Verbatim}

    \subsection{分支语句}\label{ux5206ux652fux8bedux53e5}

    Python
的控制语句格式非常简单，也很常用，为if-elif-else。如果某个分支不需要执行，可以在该分支下插入关键词None或者pass。此外，由于在Python中函数本身也是变量或对象（这点很接近函数式编程的思想），因此每个语句块用于条件判断的表达式可以是对于一个函数的调用。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}21}]:} \PY{k}{def} \PY{n+nf}{func1}\PY{p}{(}\PY{p}{)}\PY{p}{:}
             \PY{k}{return} \PY{l+m+mi}{1} \PY{o}{+} \PY{l+m+mi}{1} \PY{o}{==} \PY{l+m+mi}{2}
         
         \PY{k}{def} \PY{n+nf}{func2}\PY{p}{(}\PY{n}{num}\PY{p}{)}\PY{p}{:}
             \PY{k}{return} \PY{n}{num} \PY{k}{if} \PY{n}{num} \PY{o}{\PYZgt{}} \PY{l+m+mi}{10} \PY{k}{else} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{\PYZsq{}}
         
         \PY{n}{number} \PY{o}{=} \PY{n+nb}{eval}\PY{p}{(}\PY{n+nb}{input}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{input any number:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}\PY{p}{)}
         
         \PY{c+c1}{\PYZsh{} note here if the relationship is or function 2 may not be executed!!!!}
         \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{bingo!}\PY{l+s+s1}{\PYZsq{}}\PY{k}{if} \PY{n}{func1}\PY{p}{(}\PY{p}{)} \PY{o+ow}{and} \PY{n}{func2}\PY{p}{(}\PY{n}{number}\PY{p}{)} \PY{k}{else} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{coco!}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
input any number:8
coco!

    \end{Verbatim}

    需要注意的是，Python中没有switch语句，一般是用字典（或列表）的方式实现类似其他语言switch语句的功能。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}15}]:} \PY{n}{case} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{\PYZsq{}}
         
         \PY{n}{result} \PY{o}{=} \PY{p}{\PYZob{}}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{apple}\PY{l+s+s1}{\PYZsq{}}\PY{p}{:}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{苹果机}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,}
                  \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}}\PY{p}{:} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{boyfriend}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,}
                  \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{c}\PY{l+s+s1}{\PYZsq{}}\PY{p}{:} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{chair}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,}
                  \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{stop}\PY{l+s+s1}{\PYZsq{}} \PY{p}{:} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{break}\PY{l+s+s1}{\PYZsq{}}\PY{p}{\PYZcb{}}
         
         \PY{k}{while} \PY{n}{case} \PY{o}{!=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{stop}\PY{l+s+s1}{\PYZsq{}}\PY{p}{:}
             \PY{n}{case} \PY{o}{=} \PY{n+nb}{input}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Input ur choice:}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
             \PY{n+nb}{print}\PY{p}{(}\PY{n}{result}\PY{o}{.}\PY{n}{get}\PY{p}{(}\PY{n}{case}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{default situation is here...}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{)}    
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
Input ur choice:a
default situation is here{\ldots}
Input ur choice:apple
苹果机
Input ur choice:b
boyfriend
Input ur choice:stop
break

    \end{Verbatim}

    特别地，使用这种分支还能处理分支中多个状态且每个状态需要执行大块不同代码的情况，有点设计模式中简单工厂模式的味道。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}10}]:} \PY{k}{def} \PY{n+nf}{addstr}\PY{p}{(}\PY{n}{str1}\PY{p}{,} \PY{n}{str2}\PY{p}{)}\PY{p}{:} \PY{k}{return} \PY{n+nb}{str}\PY{p}{(}\PY{n}{str1} \PY{o}{+} \PY{n}{str2}\PY{p}{)}
         
         \PY{k}{def} \PY{n+nf}{multiply\PYZus{}str}\PY{p}{(}\PY{n}{string}\PY{p}{,} \PY{n}{a}\PY{p}{)}\PY{p}{:} \PY{k}{return} \PY{n}{string} \PY{o}{*} \PY{n}{a}
         
         \PY{k}{def} \PY{n+nf}{divide\PYZus{}str}\PY{p}{(}\PY{n}{string}\PY{p}{,} \PY{n}{a}\PY{p}{)}\PY{p}{:} \PY{k}{return} \PY{n}{string}\PY{p}{[}\PY{p}{:}\PY{n+nb}{len}\PY{p}{(}\PY{n}{string}\PY{p}{)} \PY{o}{/}\PY{o}{/} \PY{n}{a}\PY{p}{]}
         
         \PY{k}{def} \PY{n+nf}{minus\PYZus{}str}\PY{p}{(}\PY{n}{string}\PY{p}{,} \PY{n}{a}\PY{p}{)}\PY{p}{:} \PY{k}{return} \PY{n}{string}\PY{p}{[}\PY{p}{:}\PY{n+nb}{len}\PY{p}{(}\PY{n}{string}\PY{p}{)} \PY{o}{\PYZhy{}} \PY{n}{a}\PY{p}{]} \PY{k}{if} \PY{n+nb}{len}\PY{p}{(}\PY{n}{string}\PY{p}{)} \PY{o}{\PYZhy{}} \PY{n}{a} \PY{o}{\PYZgt{}} \PY{l+m+mi}{0} \PY{k}{else} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{bug}\PY{l+s+s1}{\PYZsq{}}
         
         \PY{n}{str\PYZus{}op\PYZus{}factory} \PY{o}{=} \PY{p}{\PYZob{}}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{addstr}\PY{l+s+s1}{\PYZsq{}} \PY{p}{:} \PY{n}{addstr}\PY{p}{,}
                   \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{multiply\PYZus{}str}\PY{l+s+s1}{\PYZsq{}} \PY{p}{:} \PY{n}{multiply\PYZus{}str}\PY{p}{,}
                   \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{divide\PYZus{}str}\PY{l+s+s1}{\PYZsq{}} \PY{p}{:} \PY{n}{divide\PYZus{}str}\PY{p}{,}
                   \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{minus\PYZus{}str}\PY{l+s+s1}{\PYZsq{}} \PY{p}{:} \PY{n}{minus\PYZus{}str}\PY{p}{,}
                   \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{others}\PY{l+s+s1}{\PYZsq{}} \PY{p}{:} \PY{k}{lambda} \PY{n}{x}\PY{p}{:} \PY{n}{x} \PY{o}{*}\PY{o}{*} \PY{l+m+mi}{2}\PY{p}{\PYZcb{}}
         
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{str\PYZus{}op\PYZus{}factory}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{divide\PYZus{}str}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{hello, world!}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{str\PYZus{}op\PYZus{}factory}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{minus\PYZus{}str}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{hello, world!}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+m+mi}{25}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{str\PYZus{}op\PYZus{}factory}\PY{o}{.}\PY{n}{get}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{minus\PYZus{}str}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{default\PYZus{}option}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{hello, world!}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{str\PYZus{}op\PYZus{}factory}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{others}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{(}\PY{l+m+mi}{5}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
hello,
bug
hello, worl
25

    \end{Verbatim}

    Python中的``''符号确实可以用于换行，然而并不推荐使用。更推荐的方式是对换行的表达式使用括号，这种情况在多分支中很常见。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}15}]:} \PY{n}{a}\PY{p}{,}\PY{n}{b}\PY{p}{,}\PY{n}{c}\PY{p}{,}\PY{n}{d}\PY{p}{,}\PY{n}{e}\PY{p}{,}\PY{n}{f} \PY{o}{=} \PY{p}{[}\PY{n+nb}{input}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Input anything:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}\PY{p}{]} \PY{o}{*} \PY{l+m+mi}{6}
         
         \PY{k}{if} \PY{p}{(}\PY{n}{a} \PY{o}{==} \PY{n}{b} \PY{o+ow}{and} \PY{n}{c}\PY{o}{==} \PY{n}{d} \PY{o+ow}{and}
            \PY{n}{e} \PY{o}{==} \PY{n}{f}\PY{p}{)}\PY{p}{:}
             \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{yeah!}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
         
         \PY{n}{x} \PY{o}{=} \PY{p}{(}\PY{l+m+mi}{1} \PY{o}{+} \PY{l+m+mi}{3} \PY{o}{+} \PY{l+m+mi}{4} \PY{o}{+} \PY{l+m+mi}{5} \PY{o}{+} \PY{l+m+mi}{8} \PY{o}{+} \PY{l+m+mi}{11}
              \PY{o}{+} \PY{l+m+mi}{13} \PY{o}{+} \PY{l+m+mi}{15} \PY{o}{+} \PY{l+m+mi}{17} \PY{o}{+}\PY{l+m+mi}{16}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
Input anything:hi
yeah!
93

    \end{Verbatim}

    \subsection{循环语句}\label{ux5faaux73afux8bedux53e5}

    Python的循环语句中，while循环是应对普遍情况的循环，而for循环常用于处理序列或可迭代对象。注意Python的循环为控制语句（statments），而非表达式（expression），同时不具有表达式的作用（如不能在检查条件的同时赋值）。这样可以避免``=''以及``==''的使用错误。此外，python解包也可用于for循环语句中。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}5}]:} \PY{k}{for} \PY{n}{a}\PY{p}{,}\PY{o}{*}\PY{n}{b}\PY{p}{,}\PY{n}{c} \PY{o+ow}{in} \PY{p}{[}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{c}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{d}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{h}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{e}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{l}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{l}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{o}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{]}\PY{p}{:}
            \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{,} \PY{n}{sep}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{ }\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
            
        \PY{c+c1}{\PYZsh{} A similar way using all:}
        \PY{k}{for} \PY{n+nb}{all} \PY{o+ow}{in} \PY{p}{[}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{c}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{d}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{h}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{e}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{l}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{l}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{o}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{]}\PY{p}{:}
            \PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c} \PY{o}{=} \PY{n+nb}{all}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,} \PY{n+nb}{all}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{:}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{,} \PY{n+nb}{all}\PY{p}{[}\PY{n+nb}{len}\PY{p}{(}\PY{n+nb}{all}\PY{p}{)} \PY{o}{\PYZhy{}} \PY{l+m+mi}{1}\PY{p}{]}
            \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
a ['b', 'c'] d
h ['e', 'l', 'l'] o
a ('b', 'c') d
h ('e', 'l', 'l') o

    \end{Verbatim}

    \subsubsection{可迭代变量（loop
variables）}\label{ux53efux8fedux4ee3ux53d8ux91cfloop-variables}

    如果在for语句中使用了len和range函数的组合，则速度比使用迭代慢。因此除非需要明确对应到序列对象的下标，一般不推荐使用len和range的组合或while语句。

    \textbf{注意：在迭代中需要修改元素的情况时，则需要使用for和range以及len函数的结合。}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}13}]:} \PY{c+c1}{\PYZsh{} Wrong sample:}
         \PY{n}{L} \PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{hello, kitty}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
         
         \PY{k}{for} \PY{n}{char} \PY{o+ow}{in} \PY{n}{L}\PY{p}{:}
             \PY{n}{char} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}}
             \PY{n+nb}{print}\PY{p}{(}\PY{n}{char}\PY{p}{,} \PY{n}{end}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{ }\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
         
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{L}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
a a a a a a a a a a a a ['h', 'e', 'l', 'l', 'o', ',', ' ', 'k', 'i', 't', 't', 'y']

    \end{Verbatim}

    出现这种现象的原因是，每次迭代时，char这个变量会被拷贝到一个单独空间，因此迭代变量的修改对于原对象没有影响。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}15}]:} \PY{c+c1}{\PYZsh{} Wrong sample:}
         \PY{n}{L} \PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{hello, kitty}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
         
         \PY{k}{for} \PY{n}{i} \PY{o+ow}{in} \PY{n+nb}{range}\PY{p}{(}\PY{n+nb}{len}\PY{p}{(}\PY{n}{L}\PY{p}{)}\PY{p}{)}\PY{p}{:}
             \PY{n}{L}\PY{p}{[}\PY{n}{i}\PY{p}{]} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}}
         
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{L}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']

    \end{Verbatim}

    \subsubsection{循环否分支（loop
else）}\label{ux5faaux73afux5426ux5206ux652floop-else}

    Python循环语句后的else（循环否分支，loop
else）用于运行循环语句正常退出时的情况（未被break），一个典型的用法即用于查找素数。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}1}]:} \PY{n}{trial} \PY{o}{=} \PY{k+kc}{True}
        
        \PY{k}{while} \PY{n}{trial}\PY{p}{:}
            \PY{k}{try}\PY{p}{:}
                \PY{n}{num} \PY{o}{=} \PY{n+nb}{input}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Input a number: }\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}        
                \PY{n}{num} \PY{o}{=} \PY{n+nb}{int}\PY{p}{(}\PY{n}{num}\PY{p}{)}
        
                \PY{n}{x} \PY{o}{=} \PY{n}{num} \PY{o}{/}\PY{o}{/} \PY{l+m+mi}{2} \PY{c+c1}{\PYZsh{} For some y \PYZgt{} 1}
                \PY{k}{while} \PY{n}{x} \PY{o}{\PYZgt{}} \PY{l+m+mi}{1}\PY{p}{:}
                    \PY{k}{if} \PY{n}{num} \PY{o}{\PYZpc{}} \PY{n}{x} \PY{o}{==} \PY{l+m+mi}{0}\PY{p}{:} \PY{c+c1}{\PYZsh{} Remainder}
                        \PY{n+nb}{print}\PY{p}{(}\PY{n}{num}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{has factor}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{x}\PY{p}{)}
                        \PY{k}{break} \PY{c+c1}{\PYZsh{} Skip else}
                    \PY{n}{x} \PY{o}{\PYZhy{}}\PY{o}{=} \PY{l+m+mi}{1}
                \PY{k}{else}\PY{p}{:} \PY{c+c1}{\PYZsh{} Normal exit}
                    \PY{n+nb}{print}\PY{p}{(}\PY{n}{num}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{is prime}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
            \PY{k}{except} \PY{n+ne}{ValueError}\PY{p}{:}
                \PY{n}{trial} \PY{o}{=} \PY{k+kc}{False}
        
        \PY{k}{else}\PY{p}{:}
            \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Trials terminated!}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
Input a number: 15
15 has factor 5
Input a number: 7
7 is prime
Input a number: stop
Trials terminated!

    \end{Verbatim}

    循环否分支还可以用于搜索序列中某个元素的情况。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}3}]:} \PY{n}{trial} \PY{o}{=} \PY{k+kc}{True}
        
        \PY{k}{while} \PY{n}{trial}\PY{p}{:}
            \PY{n}{lst} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{,} \PY{l+m+mi}{5}\PY{p}{,} \PY{l+m+mi}{7}\PY{p}{,} \PY{l+m+mi}{9}\PY{p}{]}
            \PY{n}{count} \PY{o}{=} \PY{l+m+mi}{1}
            
            \PY{k}{try}\PY{p}{:}
                \PY{n}{key} \PY{o}{=} \PY{n+nb}{int}\PY{p}{(}\PY{n+nb}{input}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Input a number: }\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}\PY{p}{)}
                
                \PY{c+c1}{\PYZsh{} list note empty:}
                \PY{k}{while} \PY{n}{lst}\PY{p}{:}
                    \PY{k}{if} \PY{n}{key} \PY{o}{==} \PY{n}{lst}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{:} 
                        \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{element in list, found at: }\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{count}\PY{p}{)}
                        \PY{k}{break}
                    \PY{n}{lst} \PY{o}{=} \PY{n}{lst}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{:}\PY{p}{]}
                \PY{k}{else}\PY{p}{:}
                    \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Element not in the list!}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
                
            \PY{k}{except} \PY{n+ne}{ValueError}\PY{p}{:} \PY{n}{trial} \PY{o}{=} \PY{k+kc}{False}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
Input a number: 5
element in list, found at:  1
Input a number: 6
Element not in the list!
Input a number: exit

    \end{Verbatim}

    \section{Python 操作符}\label{python-ux64cdux4f5cux7b26}

    \subsection{in 操作符}\label{in-ux64cdux4f5cux7b26}

    用来判断特定元素是否存在于特定对象（字符串列表、元组、集合或字典）中，或者循环中取特定条件迭代对象。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{n}{lou\PYZus{}jiaozhu} \PY{o}{=} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{a hacker}\PY{l+s+s2}{\PYZdq{}}
        \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{hack}\PY{l+s+s2}{\PYZdq{}} \PY{o+ow}{in} \PY{n}{lou\PYZus{}jiaozhu}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{n}{x} \PY{o}{=} \PY{n+nb}{set}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{spam}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
        \PY{n}{y} \PY{o}{=} \PY{p}{\PYZob{}}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{m}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{p}\PY{l+s+s1}{\PYZsq{}}\PY{p}{\PYZcb{}}
        
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}} \PY{o+ow}{in} \PY{n}{x}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}34}]:} \PY{k}{for} \PY{p}{(}\PY{o}{*}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{)} \PY{o+ow}{in} \PY{p}{[}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{)}\PY{p}{,}\PY{p}{(}\PY{l+m+mi}{7}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{,}\PY{l+m+mi}{9}\PY{p}{)}\PY{p}{]}\PY{p}{:}
             \PY{n+nb}{print}\PY{p}{(}\PY{o}{*}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{)}
             
         \PY{k}{for} \PY{p}{(}\PY{o}{*}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{)} \PY{o+ow}{in} \PY{p}{[}\PY{p}{(}\PY{p}{(}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{)}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{)}\PY{p}{,} \PY{p}{(}\PY{p}{(}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{)}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{)}\PY{p}{,}\PY{p}{(}\PY{p}{(}\PY{l+m+mi}{7}\PY{p}{,}\PY{l+m+mi}{8}\PY{p}{)}\PY{p}{,}\PY{l+m+mi}{9}\PY{p}{)}\PY{p}{]}\PY{p}{:}
             \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
1 2 3
4 5 6
7 8 9
[(1, 2)] 3
[(4, 5)] 6
[(7, 8)] 9

    \end{Verbatim}

    Python中常用in语句查询键是否存在于字典中，或者值是否存在于字典中。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{c+c1}{\PYZsh{} Use \PYZsq{}in\PYZsq{} operator in dictionaries:}
        \PY{n}{man} \PY{o}{=} \PY{n+nb}{dict}\PY{p}{(}\PY{n}{name}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Bob}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{job}\PY{o}{=}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{dev}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{n}{age}\PY{o}{=}\PY{l+m+mi}{40}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{name}\PY{l+s+s1}{\PYZsq{}} \PY{o+ow}{in} \PY{n}{man}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{occupation}\PY{l+s+s1}{\PYZsq{}} \PY{o+ow}{in} \PY{n}{man}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{l+m+mi}{40} \PY{o+ow}{in} \PY{n}{man}\PY{p}{)}
\end{Verbatim}


    \subsection{is 操作符}\label{is-ux64cdux4f5cux7b26}

    is操作符常用来确定对象之间的关系，用例详见第一章``共享内存''部分。

    \subsection{``*''操作符}\label{ux64cdux4f5cux7b26}

    该操作符表示将一个可迭代对象（包括序列、文件等）进行``解包''，即获取一个序列中的所有元素，然后将这些元素作为参数进行传递，产生其他用途。在赋值操作中，如果该符号出现在等号左边，则表示由程序运行时决定该变量对应的元素数量。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}19}]:} \PY{n}{seq} \PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{hello,world!}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
         \PY{n}{a}\PY{p}{,} \PY{o}{*}\PY{n}{b}\PY{p}{,} \PY{n}{c} \PY{o}{=} \PY{n}{seq}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,}\PY{n}{b}\PY{p}{,}\PY{n}{c}\PY{p}{)}
         \PY{o}{*}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c} \PY{o}{=} \PY{n}{seq}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,}\PY{n}{b}\PY{p}{,}\PY{n}{c}\PY{p}{)}
         \PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{o}{*}\PY{n}{c} \PY{o}{=} \PY{n}{seq}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,}\PY{n}{b}\PY{p}{,}\PY{n}{c}\PY{p}{)}
         
         \PY{n}{a}\PY{p}{,} \PY{o}{*}\PY{n}{b}\PY{p}{,} \PY{n}{c} \PY{o}{=} \PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{b}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
h ['e', 'l', 'l', 'o', ',', 'w', 'o', 'r', 'l', 'd'] !
['h', 'e', 'l', 'l', 'o', ',', 'w', 'o', 'r', 'l'] d !
h e ['l', 'l', 'o', ',', 'w', 'o', 'r', 'l', 'd', '!']
[1, 2, 3, 4, 5, 6, 7, 8]

    \end{Verbatim}

    然而，在赋值操作时，由`` *
''赋值的变量类型为\textbf{列表！}。它在某些情况下可能是单元素列表或空列表。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}20}]:} \PY{n}{nums} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{7}\PY{p}{]}
         \PY{n}{a}\PY{p}{,}\PY{n}{b}\PY{p}{,}\PY{n}{c}\PY{p}{,}\PY{o}{*}\PY{n}{d} \PY{o}{=} \PY{n}{nums}
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{type}\PY{p}{(}\PY{n}{d}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
<class 'list'>

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}22}]:} \PY{n}{nums} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{7}\PY{p}{]}
         \PY{n}{a}\PY{p}{,}\PY{o}{*}\PY{n}{b}\PY{p}{,}\PY{n}{c}\PY{p}{,}\PY{n}{d}\PY{p}{,}\PY{n}{e} \PY{o}{=} \PY{n}{nums}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{b}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
[]

    \end{Verbatim}

    如果赋值语句中只有一个带星号变量，则该变量后面要加上逗号。（其实可以用不同形式语句替代）。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}27}]:} \PY{n}{nums} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{7}\PY{p}{]}
         \PY{c+c1}{\PYZsh{} Must add a comma in this case.}
         \PY{o}{*}\PY{n}{lst}\PY{p}{,} \PY{o}{=} \PY{n}{nums}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{lst}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
[1, 3, 5, 7]

    \end{Verbatim}

    对字典进行解包则获得其所有的键。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}30}]:} \PY{n}{demo\PYZus{}dict} \PY{o}{=} \PY{p}{\PYZob{}}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}} \PY{p}{:} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{argentina}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}} \PY{p}{:} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{brazil}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{c}\PY{l+s+s1}{\PYZsq{}}\PY{p}{:}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{c ronaldo}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{d}\PY{l+s+s1}{\PYZsq{}}\PY{p}{:}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{dutch}\PY{l+s+s1}{\PYZsq{}}\PY{p}{\PYZcb{}}
         \PY{n+nb}{print}\PY{p}{(}\PY{o}{*}\PY{n}{demo\PYZus{}dict}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
a b c d

    \end{Verbatim}

    \section{Python方法和函数}\label{pythonux65b9ux6cd5ux548cux51fdux6570}

    属于命名空间的单个独立模块为函数，属于类属性的单个模块为方法。

    \subsection{print函数}\label{printux51fdux6570}

    print({[}thing, ..., , {[}sepa=' ', {[}end='\n', {[}file=sys.stdout,
{[}flush=False{]}{]}{]}{]}{]})函数是Python
3中常用来输出的函数。。其中，thing是需要输出的对象，sepa为对象元素之间的间隔符号，默认为空格；end为该输出结束后的符号，默认为换行；file指定输出媒介，默认为屏幕（sys.out），而flush指定输出结束后是否清理输出。

flush这个参数在一般客户端编程中不常使用，因此不额外展开探究。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}2}]:} \PY{n}{x} \PY{o}{=}  \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{monte}\PY{l+s+s1}{\PYZsq{}}
        \PY{n}{y} \PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Python}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
        \PY{n}{z} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{]}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{,} \PY{n}{z}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
monte ['P', 'y', 't', 'h', 'o', 'n'] [1, 2, 3]

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}3}]:} \PY{n}{x} \PY{o}{=}  \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{monte}\PY{l+s+s1}{\PYZsq{}}
        \PY{n}{y} \PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Python}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
        \PY{n}{z} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{]}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{,} \PY{n}{z}\PY{p}{,} \PY{n}{file}\PY{o}{=}\PY{n+nb}{open}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{resource\PYZus{}files/print\PYZus{}out\PYZus{}data.txt}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{w}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \subsubsection{Python
标准输出流sys.stdout}\label{python-ux6807ux51c6ux8f93ux51faux6d41sys.stdout}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}2}]:} \PY{k+kn}{import} \PY{n+nn}{sys}
        
        \PY{n}{screen\PYZus{}output} \PY{o}{=} \PY{n}{sys}\PY{o}{.}\PY{n}{stdout} \PY{c+c1}{\PYZsh{}1}
        \PY{n}{screen\PYZus{}error} \PY{o}{=} \PY{n}{sys}\PY{o}{.}\PY{n}{stderr} \PY{c+c1}{\PYZsh{}2}
        
        \PY{n}{sys}\PY{o}{.}\PY{n}{stdout} \PY{o}{=} \PY{n+nb}{open}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{resource\PYZus{}files/print\PYZus{}stdout\PYZus{}data.txt}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{w}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
        \PY{n}{sys}\PY{o}{.}\PY{n}{stderr} \PY{o}{=} \PY{n+nb}{open}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{resource\PYZus{}files/print\PYZus{}stderrlog\PYZus{}data.txt}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{w}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
        \PY{n}{x} \PY{o}{=}  \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{monte}\PY{l+s+s1}{\PYZsq{}}
        \PY{n}{y} \PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Python}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
        \PY{n}{z} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{]}
        
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{x}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{y}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{z}\PY{p}{)}
        
        \PY{n}{sys}\PY{o}{.}\PY{n}{stderr}\PY{o}{.}\PY{n}{write}\PY{p}{(}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Oops...}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)} \PY{o}{*} \PY{l+m+mi}{4} \PY{o}{+} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+se}{\PYZbs{}n}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
        \PY{n}{sys}\PY{o}{.}\PY{n}{stdout}\PY{o}{.}\PY{n}{close}\PY{p}{(}\PY{p}{)}
        \PY{n}{sys}\PY{o}{.}\PY{n}{stderr}\PY{o}{.}\PY{n}{close}\PY{p}{(}\PY{p}{)}
        
        \PY{n}{sys}\PY{o}{.}\PY{n}{stdout} \PY{o}{=} \PY{n}{screen\PYZus{}output} \PY{c+c1}{\PYZsh{}3}
        \PY{n}{sys}\PY{o}{.}\PY{n}{stderr} \PY{o}{=} \PY{n}{screen\PYZus{}error} \PY{c+c1}{\PYZsh{}4}
        
        \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{back to screen...}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
back to screen{\ldots}

    \end{Verbatim}

    1、2、3以及4这四行仅仅作确保流可以切换回屏幕的用途（在Jupyter
notebook中是唯一方法），其他平台或IDE中可以使用如下方法将输出流切回屏幕（默认值）。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}3}]:} \PY{n}{sys}\PY{o}{.}\PY{n}{stdout} \PY{o}{=} \PY{n}{screen\PYZus{}output}
        \PY{n}{sys}\PY{o}{.}\PY{n}{stderr} \PY{o}{=} \PY{n}{screen\PYZus{}error}
\end{Verbatim}


    \subsection{eval函数}\label{evalux51fdux6570}

    eval通常用于转化并自动决定被输入数字的数据类型。因此，在转化用户输入的到数值的情况中，它比int或者float等函数有时更推荐使用。（见第4章一个例子）:

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}5}]:} \PY{k}{while} \PY{k+kc}{True}\PY{p}{:}
            \PY{n}{msg} \PY{o}{=} \PY{n+nb}{input}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Input a nmber please! Number: }\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
            
            \PY{k}{try}\PY{p}{:}
                \PY{k}{if} \PY{n}{msg} \PY{o}{==} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{stop}\PY{l+s+s1}{\PYZsq{}} \PY{o+ow}{or} \PY{n}{msg} \PY{o}{==} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{停止}\PY{l+s+s1}{\PYZsq{}}\PY{p}{:}
                    \PY{n+nb}{print}\PY{p}{(}\PY{n}{msg}\PY{o}{.}\PY{n}{upper}\PY{p}{(}\PY{p}{)}\PY{p}{)}
                    \PY{k}{break}
                \PY{n}{digits} \PY{o}{=} \PY{n+nb}{eval}\PY{p}{(}\PY{n}{msg}\PY{p}{)}
            \PY{k}{except}\PY{p}{:}
                \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Not number! Try again!}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
            \PY{k}{else}\PY{p}{:}
                \PY{n+nb}{print}\PY{p}{(}\PY{n}{digits} \PY{o}{*}\PY{o}{*} \PY{l+m+mi}{2}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
Input a nmber please! Number: 2
4
Input a nmber please! Number: 3.8
14.44
Input a nmber please! Number: stop
STOP

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{n+nb}{eval}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{print(}\PY{l+s+s1}{\PYZdq{}}\PY{l+s+s1}{0o100}\PY{l+s+s1}{\PYZdq{}}\PY{l+s+s1}{)}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
\end{Verbatim}


    \subsection{迭代用途函数}\label{ux8fedux4ee3ux7528ux9014ux51fdux6570}

    \subsubsection{range函数}\label{rangeux51fdux6570}

    range函数用于生成一系列整数的迭代对象。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}10}]:} \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{list}\PY{p}{(}\PY{n+nb}{range}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{l+m+mi}{10}\PY{p}{,} \PY{l+m+mi}{5}\PY{p}{)}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{list}\PY{p}{(}\PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mi}{6}\PY{p}{)}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{list}\PY{p}{(}\PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mi}{3}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{)}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{list}\PY{p}{(}\PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{,} \PY{l+m+mi}{20}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
[]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2]
[10, 12, 14, 16, 18]

    \end{Verbatim}

    \subsubsection{zip函数}\label{zipux51fdux6570}

    zip函数用于拼接两个以上的等长或不等长序列，并在迭代时对每个序列的每个元素做并行处理。序列不等长时，处理的数量以最短的序列为准。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}26}]:} \PY{n}{x} \PY{o}{=} \PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{c}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{d}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}
         \PY{n}{y} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{]}
         \PY{n}{z} \PY{o}{=} \PY{p}{[}\PY{l+m+mf}{5.5}\PY{p}{,} \PY{l+m+mf}{6.6}\PY{p}{,} \PY{l+m+mf}{7.7}\PY{p}{]}
         
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{list}\PY{p}{(}\PY{n+nb}{zip}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{,} \PY{n}{z}\PY{p}{)}\PY{p}{)}\PY{p}{)}
         
         \PY{k}{for} \PY{n}{a}\PY{p}{,}\PY{n}{b}\PY{p}{,}\PY{n}{c} \PY{o+ow}{in} \PY{n+nb}{zip}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{,} \PY{n}{z}\PY{p}{)}\PY{p}{:}
             \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{,} \PY{n}{b}\PY{p}{,} \PY{n}{c}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
[('a', 1, 5.5), ('b', 2, 6.6), ('c', 3, 7.7)]
a 1 5.5
b 2 6.6
c 3 7.7

    \end{Verbatim}

    \paragraph{两（多）列列表转字典}\label{ux4e24ux591aux5217ux5217ux8868ux8f6cux5b57ux5178}

    zip函数一个很明显的用途即为将两列或多列列表转化为字典。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}1}]:} \PY{n}{lst\PYZus{}keys} \PY{o}{=} \PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{c}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{d}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}
        \PY{n}{lst\PYZus{}vals} \PY{o}{=} \PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{apple}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{banana}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{cat}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{dog}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}
        \PY{n}{lst\PYZus{}nums} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{,} \PY{l+m+mi}{5}\PY{p}{,} \PY{l+m+mi}{7}\PY{p}{,} \PY{l+m+mi}{9}\PY{p}{]}
        
        \PY{c+c1}{\PYZsh{} An easy way:}
        \PY{n}{voc\PYZus{}dict} \PY{o}{=} \PY{n+nb}{dict}\PY{p}{(}\PY{n+nb}{zip}\PY{p}{(}\PY{n}{lst\PYZus{}keys}\PY{p}{,} \PY{n+nb}{zip}\PY{p}{(}\PY{n}{lst\PYZus{}vals}\PY{p}{,} \PY{n}{lst\PYZus{}nums}\PY{p}{)}\PY{p}{)}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{voc\PYZus{}dict}\PY{p}{)}
        
        \PY{c+c1}{\PYZsh{} More complex ways:}
        \PY{n}{voc\PYZus{}dict} \PY{o}{=} \PY{p}{\PYZob{}}\PY{n}{k}\PY{p}{:}\PY{n}{v} \PY{k}{for} \PY{p}{(}\PY{n}{k}\PY{p}{,}\PY{n}{v}\PY{p}{)} \PY{o+ow}{in} \PY{n+nb}{zip}\PY{p}{(}\PY{n}{lst\PYZus{}keys}\PY{p}{,} \PY{n+nb}{zip}\PY{p}{(}\PY{n}{lst\PYZus{}vals}\PY{p}{,} \PY{n}{lst\PYZus{}nums}\PY{p}{)}\PY{p}{)}\PY{p}{\PYZcb{}}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{voc\PYZus{}dict}\PY{p}{)}
        
        \PY{n}{voc\PYZus{}dict} \PY{o}{=} \PY{p}{\PYZob{}}\PY{p}{\PYZcb{}}
        \PY{k}{for} \PY{n}{k}\PY{p}{,} \PY{n}{v} \PY{o+ow}{in} \PY{n+nb}{zip}\PY{p}{(}\PY{n}{lst\PYZus{}keys}\PY{p}{,} \PY{n+nb}{zip}\PY{p}{(}\PY{n}{lst\PYZus{}vals}\PY{p}{,} \PY{n}{lst\PYZus{}nums}\PY{p}{)}\PY{p}{)}\PY{p}{:}
            \PY{n}{voc\PYZus{}dict}\PY{p}{[}\PY{n}{k}\PY{p}{]} \PY{o}{=} \PY{n}{v}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{voc\PYZus{}dict}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
\{'a': ('apple', 1), 'b': ('banana', 3), 'c': ('cat', 5), 'd': ('dog', 7)\}
\{'a': ('apple', 1), 'b': ('banana', 3), 'c': ('cat', 5), 'd': ('dog', 7)\}
\{'a': ('apple', 1), 'b': ('banana', 3), 'c': ('cat', 5), 'd': ('dog', 7)\}

    \end{Verbatim}

    \subsubsection{enumerate函数}\label{enumerateux51fdux6570}

    enumerate函数对于序列的每个对象，返回一个元组，包含下标及其对应的元素。它的本质是一个迭代器（generator）。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}4}]:} \PY{n}{lst} \PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{射鸟英雄传}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
        
        \PY{k}{for} \PY{n}{ele} \PY{o+ow}{in} \PY{n+nb}{enumerate}\PY{p}{(}\PY{n}{lst}\PY{p}{)}\PY{p}{:}
            \PY{n+nb}{print}\PY{p}{(}\PY{n}{ele}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
(0, '射')
(1, '鸟')
(2, '英')
(3, '雄')
(4, '传')

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}7}]:} \PY{n}{lst} \PY{o}{=} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{,} \PY{l+m+mi}{5}\PY{p}{,} \PY{l+m+mi}{7}\PY{p}{]}
        \PY{n}{enum} \PY{o}{=} \PY{n+nb}{enumerate}\PY{p}{(}\PY{n}{lst}\PY{p}{)}
        
        \PY{k}{try}\PY{p}{:}
            \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{enum}\PY{p}{)}\PY{p}{)}
            \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{enum}\PY{p}{)}\PY{p}{)}
            \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{enum}\PY{p}{)}\PY{p}{)}
            \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{enum}\PY{p}{)}\PY{p}{)}
            \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{enum}\PY{p}{)}\PY{p}{)}
        \PY{k}{except} \PY{n+ne}{StopIteration}\PY{p}{:}
            \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{迭代完了！}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
(0, 1)
(1, 3)
(2, 5)
(3, 7)
迭代完了！

    \end{Verbatim}

    \subsubsection{\_\_next\_\_函数}\label{next__ux51fdux6570}

    \_\_next\_\_函数用于获取可迭代对象的下一个元素，比如在文件处理中，它的功能类似于readline()方法。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}12}]:} \PY{n}{f} \PY{o}{=} \PY{n+nb}{open}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{resource\PYZus{}files/russian\PYZus{}world\PYZus{}cup.txt}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{f}\PY{o}{.}\PY{n+nf+fm}{\PYZus{}\PYZus{}next\PYZus{}\PYZus{}}\PY{p}{(}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{f}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{f}\PY{p}{)}\PY{p}{)}
         \PY{n}{f}\PY{o}{.}\PY{n}{close}\PY{p}{(}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
原标题：俄罗斯世界杯的经济学：盈利将创纪录 美中墨球迷最大方

参考消息网7月7日报道 俄罗斯军事观察网7月5日报道称，俄罗斯世界杯已被评为史上最为盈利的一届世界杯。利润不仅来自各支球队参加比赛的直接支出，还得益于电视转播、广告出售和球迷在俄花销等。

据美国《纽约时报》报道，国际足联在2010年南非世界杯上赚了39亿美元，在2014年巴西世界杯上赚了48亿美元。


    \end{Verbatim}

    \textbf{注意：有的对象类内定义了\_\_next\_\_函数，则不支持使用next()函数对其调用。}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}34}]:} \PY{k+kn}{import} \PY{n+nn}{os}
         
         \PY{n}{dir\PYZus{}call} \PY{o}{=} \PY{n}{os}\PY{o}{.}\PY{n}{popen}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{dir}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{dir\PYZus{}call}\PY{o}{.}\PY{n+nf+fm}{\PYZus{}\PYZus{}next\PYZus{}\PYZus{}}\PY{p}{(}\PY{p}{)}\PY{p}{)}
         
         \PY{l+s+sd}{\PYZdq{}\PYZdq{}\PYZdq{}}
         \PY{l+s+sd}{\PYZsh{} This:}
         \PY{l+s+sd}{next(dir\PYZus{}call)}
         \PY{l+s+sd}{\PYZsh{}would be wrong in this case.}
         \PY{l+s+sd}{\PYZdq{}\PYZdq{}\PYZdq{}}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
 驱动器 F 中的卷是 VM


    \end{Verbatim}

\begin{Verbatim}[commandchars=\\\{\}]
{\color{outcolor}Out[{\color{outcolor}34}]:} '\textbackslash{}n\# This:\textbackslash{}nnext(dir\_call)\textbackslash{}n\#would be wrong in this case.\textbackslash{}n'
\end{Verbatim}
            
    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}33}]:} \PY{c+c1}{\PYZsh{} But can use iter function to change it to iterator:}
         \PY{k+kn}{import} \PY{n+nn}{os}
         
         \PY{n}{dir\PYZus{}call} \PY{o}{=} \PY{n+nb}{iter}\PY{p}{(}\PY{n}{os}\PY{o}{.}\PY{n}{popen}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{dir}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{dir\PYZus{}call}\PY{o}{.}\PY{n+nf+fm}{\PYZus{}\PYZus{}next\PYZus{}\PYZus{}}\PY{p}{(}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{dir\PYZus{}call}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
 驱动器 F 中的卷是 VM

 卷的序列号是 12F2-6ABC


    \end{Verbatim}

    \subsubsection{iter 函数}\label{iter-ux51fdux6570}

    iter函数用于将序列对象转化为可迭代对象。可用于列表以及字典，甚至是range对象以及enumerate对象，常与next函数结合使用。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}37}]:} \PY{n}{losts\PYZus{}lst} \PY{o}{=} \PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Brazil}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Germany}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Japan}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Korea}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}
         \PY{l+s+sd}{\PYZdq{}\PYZdq{}\PYZdq{}}
         \PY{l+s+sd}{\PYZsh{} This won\PYZsq{}t be running, since}
         \PY{l+s+sd}{\PYZsh{} it is still not iterable objs yet. }
         \PY{l+s+sd}{print(next(losts\PYZus{}lst))}
         \PY{l+s+sd}{\PYZdq{}\PYZdq{}\PYZdq{}}
         
         \PY{n}{losts\PYZus{}iter} \PY{o}{=} \PY{n+nb}{iter}\PY{p}{(}\PY{n}{losts\PYZus{}lst}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{losts\PYZus{}iter} \PY{o+ow}{is} \PY{n}{losts\PYZus{}iter}\PY{o}{.}\PY{n+nf+fm}{\PYZus{}\PYZus{}iter\PYZus{}\PYZus{}}\PY{p}{(}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{losts\PYZus{}iter}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{losts\PYZus{}iter}\PY{o}{.}\PY{n+nf+fm}{\PYZus{}\PYZus{}next\PYZus{}\PYZus{}}\PY{p}{(}\PY{p}{)}\PY{p}{)}
         
         \PY{c+c1}{\PYZsh{} Example of applied on an enumerator:}
         \PY{n}{losts\PYZus{}enum} \PY{o}{=} \PY{n+nb}{enumerate}\PY{p}{(}\PY{n}{losts\PYZus{}lst}\PY{p}{)}
         \PY{n}{losts\PYZus{}iter} \PY{o}{=} \PY{n+nb}{iter}\PY{p}{(}\PY{n}{losts\PYZus{}enum}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{losts\PYZus{}iter}\PY{o}{.}\PY{n+nf+fm}{\PYZus{}\PYZus{}next\PYZus{}\PYZus{}}\PY{p}{(}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{losts\PYZus{}iter}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
True
Brazil
Germany
(0, 'Brazil')
(1, 'Germany')

    \end{Verbatim}

    由于字典时，可以返回一个键。（类似于对于字典的解包操作）

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}25}]:} \PY{n}{teams\PYZus{}dict} \PY{o}{=} \PY{p}{\PYZob{}}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}} \PY{p}{:} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Argentina}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{b}\PY{l+s+s1}{\PYZsq{}}\PY{p}{:} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{brazil}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{c}\PY{l+s+s1}{\PYZsq{}} \PY{p}{:} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{c ronardo}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{d}\PY{l+s+s1}{\PYZsq{}} \PY{p}{:} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{dutch}\PY{l+s+s1}{\PYZsq{}}\PY{p}{\PYZcb{}}
         \PY{n}{teams\PYZus{}iter} \PY{o}{=} \PY{n+nb}{iter}\PY{p}{(}\PY{n}{teams\PYZus{}dict}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{teams\PYZus{}iter}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
a

    \end{Verbatim}

    \subsubsection{filter 函数}\label{filter-ux51fdux6570}

    filter函数用于过滤（序列中）符合特定条件的元素，并返回其迭代。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}38}]:} \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{list}\PY{p}{(}\PY{n+nb}{filter}\PY{p}{(}\PY{n+nb}{bool}\PY{p}{,} \PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{a}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{k+kc}{None}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{hi}\PY{l+s+s1}{\PYZsq{}}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{]}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
['a', 'hi', 1, 2, 3]

    \end{Verbatim}

    \subsubsection{迭代函数之间的区别}\label{ux8fedux4ee3ux51fdux6570ux4e4bux95f4ux7684ux533aux522b}

    range函数与zip、map以及filter函数不同，它支持对于同一个范围对象的多个迭代，其它函数则不支持。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}5}]:} \PY{n}{R} \PY{o}{=} \PY{n+nb}{range}\PY{p}{(}\PY{l+m+mi}{5}\PY{p}{)}
        \PY{n}{it1} \PY{o}{=} \PY{n+nb}{iter}\PY{p}{(}\PY{n}{R}\PY{p}{)}
        
        \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Iterator 1: }\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{it1}\PY{p}{)}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{it1}\PY{p}{)}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{it1}\PY{p}{)}\PY{p}{)}
        
        \PY{n}{it2} \PY{o}{=} \PY{n+nb}{iter}\PY{p}{(}\PY{n}{R}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Iterator 2: }\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{it2}\PY{p}{)}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{it2}\PY{o}{.}\PY{n+nf+fm}{\PYZus{}\PYZus{}next\PYZus{}\PYZus{}}\PY{p}{(}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
Iterator 1: 
0
1
2
Iterator 2: 
0
1

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}18}]:} \PY{n}{z\PYZus{}arr} \PY{o}{=} \PY{n+nb}{zip}\PY{p}{(}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{3}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{4}\PY{p}{,}\PY{l+m+mi}{5}\PY{p}{,}\PY{l+m+mi}{6}\PY{p}{]}\PY{p}{)}
         \PY{n}{it1} \PY{o}{=} \PY{n+nb}{iter}\PY{p}{(}\PY{n}{z\PYZus{}arr}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{next}\PY{p}{(}\PY{n}{it1}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{it1}\PY{o}{.}\PY{n+nf+fm}{\PYZus{}\PYZus{}next\PYZus{}\PYZus{}}\PY{p}{(}\PY{p}{)}\PY{p}{)}
         
         \PY{n}{it2} \PY{o}{=} \PY{n+nb}{iter}\PY{p}{(}\PY{n}{z\PYZus{}arr}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{it2}\PY{o}{.}\PY{n+nf+fm}{\PYZus{}\PYZus{}next\PYZus{}\PYZus{}}\PY{p}{(}\PY{p}{)}\PY{p}{)}
         
         \PY{n}{maps} \PY{o}{=} \PY{n+nb}{map}\PY{p}{(}\PY{n+nb}{abs}\PY{p}{,} \PY{p}{[}\PY{o}{\PYZhy{}}\PY{l+m+mi}{3}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{]}\PY{p}{)}
         \PY{n}{it1} \PY{o}{=} \PY{n+nb}{iter}\PY{p}{(}\PY{n}{maps}\PY{p}{)}
         \PY{n}{it2} \PY{o}{=} \PY{n+nb}{iter}\PY{p}{(}\PY{n}{maps}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{it1}\PY{o}{.}\PY{n+nf+fm}{\PYZus{}\PYZus{}next\PYZus{}\PYZus{}}\PY{p}{(}\PY{p}{)}\PY{p}{,} \PY{n+nb}{next}\PY{p}{(}\PY{n}{it1}\PY{p}{)}\PY{p}{,} \PY{n+nb}{next}\PY{p}{(}\PY{n}{it2}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
(1, 4)
(2, 5)
(3, 6)
3 0 2

    \end{Verbatim}

    \subsection{map函数}\label{mapux51fdux6570}

    Python的map函数根据给定的函数计算每个迭代变量，常与列表等结合使用返回一系列值。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{list}\PY{p}{(}\PY{n+nb}{map}\PY{p}{(}\PY{n+nb}{abs}\PY{p}{,}\PY{p}{[}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{,}\PY{o}{\PYZhy{}}\PY{l+m+mi}{2}\PY{p}{,}\PY{l+m+mi}{0}\PY{p}{,}\PY{l+m+mi}{1}\PY{p}{,}\PY{l+m+mi}{2}\PY{p}{]}\PY{p}{)}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{n}{M} \PY{o}{=} \PY{p}{[}
            \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{,} \PY{l+m+mi}{3}\PY{p}{]}\PY{p}{,}
            \PY{p}{[}\PY{l+m+mi}{4}\PY{p}{,} \PY{l+m+mi}{5}\PY{p}{,} \PY{l+m+mi}{6}\PY{p}{]}\PY{p}{,}
            \PY{p}{[}\PY{l+m+mi}{7}\PY{p}{,} \PY{l+m+mi}{8}\PY{p}{,} \PY{l+m+mi}{9}\PY{p}{]}
        \PY{p}{]}
        
        \PY{n}{sums} \PY{o}{=} \PY{n+nb}{list}\PY{p}{(}\PY{n+nb}{map}\PY{p}{(}\PY{n+nb}{sum}\PY{p}{,} \PY{n}{M}\PY{p}{)}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{sums}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}28}]:} \PY{n+nb}{print}\PY{p}{(}\PY{n+nb}{list}\PY{p}{(}\PY{n+nb}{map}\PY{p}{(}\PY{n+nb}{ord}\PY{p}{,} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{hello,kitty!}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{)}\PY{p}{)}
         
         \PY{c+c1}{\PYZsh{} This is the same as:}
         \PY{n}{intarr} \PY{o}{=} \PY{p}{[}\PY{p}{]}
         \PY{k}{for} \PY{n}{char} \PY{o+ow}{in} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{hello,kitty!}\PY{l+s+s1}{\PYZsq{}}\PY{p}{:}
             \PY{n}{intarr}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n+nb}{ord}\PY{p}{(}\PY{n}{char}\PY{p}{)}\PY{p}{)}
         \PY{n+nb}{print}\PY{p}{(}\PY{n}{intarr}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
[104, 101, 108, 108, 111, 44, 107, 105, 116, 116, 121, 33]
[104, 101, 108, 108, 111, 44, 107, 105, 116, 116, 121, 33]

    \end{Verbatim}

    \section{Python系统编程}\label{pythonux7cfbux7edfux7f16ux7a0b}

    \subsection{一些常用的系统信息}\label{ux4e00ux4e9bux5e38ux7528ux7684ux7cfbux7edfux4fe1ux606f}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{c+c1}{\PYZsh{} A first Python script}
        \PY{k+kn}{import} \PY{n+nn}{sys}                  \PY{c+c1}{\PYZsh{} Load a library module}
        \PY{k+kn}{import} \PY{n+nn}{os}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{sys}\PY{o}{.}\PY{n}{version}\PY{o}{.}\PY{n}{split}\PY{p}{(}\PY{p}{)}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{sys}\PY{o}{.}\PY{n}{platform}\PY{p}{)}
        \PY{c+c1}{\PYZsh{} 获得当前项目路径}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{os}\PY{o}{.}\PY{n}{getcwd}\PY{p}{(}\PY{p}{)}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{l+m+mi}{2} \PY{o}{*}\PY{o}{*} \PY{l+m+mi}{32}\PY{p}{)}              \PY{c+c1}{\PYZsh{} Raise 2 to a power}
        \PY{n}{x} \PY{o}{=} \PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{Spam!}\PY{l+s+s1}{\PYZsq{}}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{x} \PY{o}{*} \PY{l+m+mi}{8}\PY{p}{)}                \PY{c+c1}{\PYZsh{} String repetition}
\end{Verbatim}


    模块制作和导入可以参考：https://my.oschina.net/Samyan/blog/872413
,其中，可以通过打出sys.path了解系统模块的导入位置。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{k+kn}{import} \PY{n+nn}{pprint}
        \PY{n}{pprint}\PY{o}{.}\PY{n}{pprint}\PY{p}{(}\PY{n}{sys}\PY{o}{.}\PY{n}{path}\PY{p}{)}
\end{Verbatim}


    \subsection{加载模块的若干注意项}\label{ux52a0ux8f7dux6a21ux5757ux7684ux82e5ux5e72ux6ce8ux610fux9879}

    python3中，重载模块需要显式调用imp包中的reload模块

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{c+c1}{\PYZsh{}只加载一次：}
        \PY{n}{a} \PY{o}{=} \PY{l+m+mi}{999}
        \PY{k+kn}{import} \PY{n+nn}{script1}
        \PY{k+kn}{import} \PY{n+nn}{script1}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{k+kn}{from} \PY{n+nn}{imp} \PY{k}{import} \PY{n}{reload}
        \PY{n}{reload}\PY{p}{(}\PY{n}{script1}\PY{p}{)}
\end{Verbatim}


    本质上来说，Python中的每个模块是一个命名空间（namespace），它包含一系列属性（attributes）。属性是指与特定对象有关的变量名，具体可以包含这个命名空间中的类、方法、函数以及变量。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{n+nb}{dir}\PY{p}{(}\PY{n}{script1}\PY{p}{)}
\end{Verbatim}


    \textbf{注意：使用exec或from加载模块时，可能会导致程序中的现有信息被改写。}

    可以使用exec(py\_commends)函数与Python
I/O结合的方式读取python模块。但是与from一样，这可能会导致变量被隐式重写。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor} }]:} \PY{n}{a} \PY{o}{=} \PY{l+m+mi}{999}
        \PY{n}{exec}\PY{p}{(}\PY{n+nb}{open}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{script1.py}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{o}{.}\PY{n}{read}\PY{p}{(}\PY{p}{)}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{a is:}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{a}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}7}]:} \PY{n}{a} \PY{o}{=} \PY{l+m+mi}{999}
        \PY{k+kn}{from} \PY{n+nn}{imp} \PY{k}{import} \PY{n}{reload}
        \PY{k+kn}{from} \PY{n+nn}{script1} \PY{k}{import} \PY{o}{*}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{a}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
win32
4294967296
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
dead parrot sketch
dead

    \end{Verbatim}

    \subsection{os.popen 函数}\label{os.popen-ux51fdux6570}

    使用os.popen模块可以对于系统信息进行类似文档读取的操作。

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}5}]:} \PY{k+kn}{import} \PY{n+nn}{os}
        
        \PY{n}{file} \PY{o}{=} \PY{n}{os}\PY{o}{.}\PY{n}{popen}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{dir}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{file}\PY{o}{.}\PY{n}{readline}\PY{p}{(}\PY{p}{)}\PY{p}{)}
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{file}\PY{o}{.}\PY{n}{read}\PY{p}{(}\PY{l+m+mi}{50}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
 驱动器 F 中的卷是 VM

 卷的序列号是 12F2-6ABC

 F:\textbackslash{}Git\textbackslash{}Code\_devs\textbackslash{}Py\_Jupyter\_Wo

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}6}]:} \PY{k+kn}{import} \PY{n+nn}{os}
        
        \PY{k}{for} \PY{n}{line} \PY{o+ow}{in} \PY{n}{os}\PY{o}{.}\PY{n}{popen}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{dir}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{:}
            \PY{n+nb}{print}\PY{p}{(}\PY{n}{line}\PY{o}{.}\PY{n}{rstrip}\PY{p}{(}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
 驱动器 F 中的卷是 VM
 卷的序列号是 12F2-6ABC

 F:\textbackslash{}Git\textbackslash{}Code\_devs\textbackslash{}Py\_Jupyter\_Workspace\textbackslash{}Python\_Advanced\_programming 的目录

2018/07/06  10:55    <DIR>          .
2018/07/06  10:55    <DIR>          ..
2018/06/17  11:24    <DIR>          .ipynb\_checkpoints
2018/06/16  17:30    <DIR>          img\_notes
2018/07/06  10:55         1,692,209 Python\_learning.ipynb
2018/06/16  17:30            35,922 Python\_primary\_data\_types.png
2018/06/30  16:54    <DIR>          Py\_programs
2018/06/16  17:30                83 README.md
2018/07/05  21:54    <DIR>          resource\_files
2018/06/16  17:30               310 script1.py
2018/06/16  17:30    <DIR>          \_\_pycache\_\_
               4 个文件      1,728,524 字节
               7 个目录 218,510,229,504 可用字节

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}9}]:} \PY{k+kn}{import} \PY{n+nn}{os}
        
        \PY{n+nb}{print}\PY{p}{(}\PY{n}{os}\PY{o}{.}\PY{n}{system}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{systeminfo}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
0

    \end{Verbatim}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}10}]:} \PY{k}{for} \PY{n}{line} \PY{o+ow}{in} \PY{n}{os}\PY{o}{.}\PY{n}{popen}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{systeminfo}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{:}
             \PY{n+nb}{print}\PY{p}{(}\PY{n}{line}\PY{o}{.}\PY{n}{rstrip}\PY{p}{(}\PY{p}{)}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]

主机名:           DESKTOP-DL4Q4RQ
OS 名称:          Microsoft Windows 10 家庭中文版
OS 版本:          10.0.14393 暂缺 Build 14393
OS 制造商:        Microsoft Corporation
OS 配置:          独立工作站
OS 构件类型:      Multiprocessor Free
注册的所有人:     暂缺
注册的组织:       暂缺
产品 ID:          00342-34609-17251-AAOEM
初始安装日期:     2017/7/7, 02:04:54
系统启动时间:     2018/7/2, 23:15:40
系统制造商:       Dell Inc.
系统型号:         Vostro 3559
系统类型:         x64-based PC
处理器:           安装了 1 个处理器。
                  [01]: Intel64 Family 6 Model 78 Stepping 3 GenuineIntel \textasciitilde{}2401 Mhz
BIOS 版本:        Dell Inc. 1.2.1, 2016/8/5
Windows 目录:     C:\textbackslash{}Windows
系统目录:         C:\textbackslash{}Windows\textbackslash{}system32
启动设备:         \textbackslash{}Device\textbackslash{}HarddiskVolume1
系统区域设置:     zh-cn;中文(中国)
输入法区域设置:   zh-cn;中文(中国)
时区:             (UTC+10:00) 堪培拉，墨尔本，悉尼
物理内存总量:     8,084 MB
可用的物理内存:   3,495 MB
虚拟内存: 最大值: 9,364 MB
虚拟内存: 可用:   3,789 MB
虚拟内存: 使用中: 5,575 MB
页面文件位置:     C:\textbackslash{}pagefile.sys
域:               WORKGROUP
登录服务器:       \textbackslash{}\textbackslash{}DESKTOP-DL4Q4RQ
修补程序:         安装了 2 个修补程序。
                  [01]: KB3199209
                  [02]: KB3197954
网卡:             安装了 5 个 NIC。
                  [01]: Intel(R) Dual Band Wireless-AC 3160
                      连接名:      WLAN
                      启用 DHCP:   是
                      DHCP 服务器: 100.97.80.1
                      IP 地址
                        [01]: 100.97.83.210
                        [02]: fe80::119b:6d3e:4c37:2da7
                  [02]: Realtek PCIe GBE Family Controller
                      连接名:      以太网
                      状态:        媒体连接已中断
                  [03]: Bluetooth Device (Personal Area Network)
                      连接名:      蓝牙网络连接
                      状态:        媒体连接已中断
                  [04]: VirtualBox Host-Only Ethernet Adapter
                      连接名:      VirtualBox Host-Only Network
                      启用 DHCP:   否
                      IP 地址
                        [01]: 192.168.56.1
                        [02]: fe80::b150:1476:3a41:53ec
                  [05]: Cisco AnyConnect Secure Mobility Client Virtual Miniport Adapter for Windows x64
                      连接名:      以太网 2
                      状态:        没有硬件
Hyper-V 要求:     虚拟机监视器模式扩展: 是
                  固件中已启用虚拟化: 是
                  二级地址转换: 是
                  数据执行保护可用: 是

    \end{Verbatim}

    \section{Python网络编程}\label{pythonux7f51ux7edcux7f16ux7a0b}

    \begin{Verbatim}[commandchars=\\\{\}]
{\color{incolor}In [{\color{incolor}12}]:} \PY{k+kn}{from} \PY{n+nn}{urllib}\PY{n+nn}{.}\PY{n+nn}{request} \PY{k}{import} \PY{n}{urlopen}
         
         \PY{k}{for} \PY{n}{line} \PY{o+ow}{in} \PY{n}{urlopen}\PY{p}{(}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{http://home.rmi.net/\PYZti{}lutz}\PY{l+s+s1}{\PYZsq{}}\PY{p}{)}\PY{p}{:}
             \PY{n+nb}{print}\PY{p}{(}\PY{n}{line}\PY{p}{)}
\end{Verbatim}


    \begin{Verbatim}[commandchars=\\\{\}]
b'<HTML>\textbackslash{}n'
b'<HEAD>\textbackslash{}n'
b'<META HTTP-EQUIV="Refresh" CONTENT="20; URL=http://learning-python.com/books">\textbackslash{}n'
b'\textbackslash{}n'
b'<link rel="shortcut icon" type="image/x-icon" href="http://www.rmi.net/\textasciitilde{}lutz/favicon.ico" />\textbackslash{}n'
b'\textbackslash{}n'
b'<title>Site Redirection Page: index.html</title>\textbackslash{}n'
b'\textbackslash{}n'
b'<!-- monitor traffic to redirect pages, so know when safe to delete -->\textbackslash{}n'
b'<script>\textbackslash{}n'
b"  (function(i,s,o,g,r,a,m)\{i['GoogleAnalyticsObject']=r;i[r]=i[r]||function()\{\textbackslash{}n"
b'  (i[r].q=i[r].q||[]).push(arguments)\},i[r].l=1*new Date();a=s.createElement(o),\textbackslash{}n'
b'  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\textbackslash{}n'
b"  \})(window,document,'script','//www.google-analytics.com/analytics.js','ga');\textbackslash{}n"
b'\textbackslash{}n'
b"  ga('create', 'UA-69509665-1', 'auto');\textbackslash{}n"
b"  ga('send', 'pageview');\textbackslash{}n"
b'</script>\textbackslash{}n'
b'</HEAD>\textbackslash{}n'
b'\textbackslash{}n'
b'<BODY bgcolor=cornsilk>\textbackslash{}n'
b'\textbackslash{}n'
b'<P>\textbackslash{}n'
b'<TABLE>\textbackslash{}n'
b'\textbackslash{}n'
b'<TR>\textbackslash{}n'
b'\textbackslash{}n'
b'<TD>\textbackslash{}n'
b'<TABLE>\textbackslash{}n'
b'<TR>\textbackslash{}n'
b'<P>\textbackslash{}n'
b'<A HREF="http://learning-python.com/books/about-lp.html">\textbackslash{}n'
b'<IMG src="ora-lp5e-tiny.jpg" alt="[LP5E]" hspace=6 vspace=3>\textbackslash{}n'
b'</A>\textbackslash{}n'
b'</TR>\textbackslash{}n'
b'\textbackslash{}n'
b'<TR>\textbackslash{}n'
b'<P>\textbackslash{}n'
b'<A HREF="http://learning-python.com/books/about-pp.html">\textbackslash{}n'
b'<IMG src="ora-pp4e-tiny.jpg" alt="[PP4E]" hspace=6 vspace=3>\textbackslash{}n'
b'</A>\textbackslash{}n'
b'</TR>\textbackslash{}n'
b'</TABLE>\textbackslash{}n'
b'</TD>\textbackslash{}n'
b'\textbackslash{}n'
b'\textbackslash{}n'
b'<TD>\&nbsp;\&nbsp;\&nbsp;\&nbsp;\textbackslash{}n'
b'<TD>\textbackslash{}n'
b'<H1>This Site Has Moved</H1>\textbackslash{}n'
b'\textbackslash{}n'
b'<P>\textbackslash{}n'
b'The resource you accessed now lives at this address:\textbackslash{}n'
b'\textbackslash{}n'
b'<PRE>\textbackslash{}n'
b'    <B><A HREF="http://learning-python.com/books">http://learning-python.com/books</A></B>\textbackslash{}n'
b'</PRE>\textbackslash{}n'
b'\textbackslash{}n'
b'<P>\textbackslash{}n'
b'To jump to this item, either click its new address above, \textbackslash{}n'
b'or wait to be redirected automatically in 20 seconds.\textbackslash{}n'
b'\textbackslash{}n'
b'<P>\textbackslash{}n'
b'Please update any links accordingly. \textbackslash{}n'
b'Why the move?\&mdash;read the <A HREF="earthlink-outage.html">backstory</A>.\textbackslash{}n'
b'\textbackslash{}n'
b'</P>\textbackslash{}n'
b'</TD>\textbackslash{}n'
b'</TR>\textbackslash{}n'
b'</TABLE>\textbackslash{}n'
b'</P>\textbackslash{}n'
b'\textbackslash{}n'
b'<HR>\textbackslash{}n'
b'<I>\textbackslash{}n'
b'This page was generated on Oct-30-2015 by \textbackslash{}n'
b'<A HREF="http://learning-python.com/books/about-pp4e.html">PP4E</A> book example\textbackslash{}n'
b'<A HREF="site-forward.py">site-forward.py</A> using this \textbackslash{}n'
b'<A HREF="template.html">template</A>.\textbackslash{}n'
b'</I>\textbackslash{}n'
b'</P>\textbackslash{}n'
b'\textbackslash{}n'
b'</BODY>\textbackslash{}n'
b'</HTML>'

    \end{Verbatim}

    \section{参考文献}\label{ux53c2ux8003ux6587ux732e}


    % Add a bibliography block to the postdoc
    
    
    
    \end{document}
